﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace QD.Tools
{
    public static class DataTableHelper
    {
        public static void InitObj(this DataRow dr, object obj, params string[] ignoreProperty)
        {
            PropertyInfo[] pInfos = obj.GetType().GetProperties();
            foreach (PropertyInfo info in pInfos)
            {
                if (!ignoreProperty.Contains(info.Name))
                {
                    if (info.PropertyType.IsValueType || info.PropertyType == typeof(string))
                    {
                        if (dr.Table.Columns.Contains(info.Name))
                        {
                            info.SetValue(obj, dr[info.Name].ChanageType(info.PropertyType));
                        }
                    }
                    else if (info.PropertyType == typeof(Byte[]) && dr[info.Name] != DBNull.Value)
                    {
                        info.SetValue(obj, (Byte[])dr[info.Name]);
                    }
                }
            }
        }

        public static void AddColumn(this DataTable dt, string colName)
        {
            dt.Columns.Add(colName);
        }

        public static void AddColumn(this DataTable dt, string colName, object value)
        {
            dt.Columns.Add(colName);
            foreach (DataRow dr in dt.Rows)
            {
                dr[colName] = value;
            }
        }

        public static void AddColumn(this DataTable dt, string colName, Type type)
        {
            DataColumn col = new DataColumn(colName, type);
            dt.Columns.Add(col);
        }

        public static void AddColumn(this DataTable dt, string colName, Type type, object value)
        {
            DataColumn col = new DataColumn(colName, type);
            dt.Columns.Add(col);
            foreach (DataRow dr in dt.Rows)
            {
                dr[col.ColumnName] = value;
            }
        }

        /// <summary>
        /// 取DataTable中指定状态的行
        /// </summary>
        public static DataRow[] GetRows(this DataTable dt, bool isContains, params DataRowState[] states)
        {
            List<DataRow> rowList = new List<DataRow>();
            foreach (DataRow dr in dt.Rows)
            {
                if (states.Contains(dr.RowState) == isContains)
                {
                    rowList.Add(dr);
                }
            }

            return rowList.ToArray();
        }

        /// <summary>
        /// 判断某单元格的值是否为空
        /// </summary>
        public static bool IsEmpty(this DataRow dr, string colName)
        {
            if (dr[colName] == DBNull.Value || dr[colName].ToString().Trim().Length == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool GetCellValueToBool(this DataRow dr, string colName)
        {
            if (dr.IsEmpty(colName))
            {
                return false;
            }
            else
            {
                return Convert.ToBoolean(dr[colName]);
            }
        }

        public static string GetCellValueToString(this DataRow dr, string colName)
        {
            if (dr.IsEmpty(colName))
            {
                return string.Empty;
            }
            else
            {
                return dr[colName].ToString();
            }
        }

        /// <summary>
        /// 验证是否有相同的列值
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="columnCode"></param>
        /// <returns></returns>
        public static bool IsSameColumnValue(this DataTable dt, string columnCode, ref string firstSameValue)
        {
            bool isSame = false;
            foreach (DataRow dr in dt.Rows)
            {
                if (isSame)
                {
                    break;
                }
                else
                {
                    if (dr.RowState != DataRowState.Deleted)
                    {
                        foreach (DataRow _dr in dt.Rows)
                        {
                            if (dr != _dr && _dr.RowState != DataRowState.Deleted)
                            {
                                if (dr[columnCode].ToString() == _dr[columnCode].ToString())
                                {
                                    firstSameValue = dr[columnCode].ToString();
                                    isSame = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return isSame;
        }

        /// <summary>
        /// 取DataTable中前指定的数据
        /// </summary>
        /// <param name="oDT"></param>
        /// <param name="TopItem"></param>
        /// <returns></returns>
        public static DataTable GetTopData(this DataTable oDT, int TopItem)
        {
            if (oDT.Rows.Count < TopItem) return oDT;

            DataTable NewTable = oDT.Clone();
            DataRow[] rows = oDT.Select("1=1");
            for (int i = 0; i < TopItem; i++)
            {
                NewTable.ImportRow((DataRow)rows[i]);
            }
            return NewTable;
        }
    }
}
